package znbkTopicType.content;

import android.content.Context;
import android.os.Handler;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.lancooznbk.R;
import com.example.lancooznbk.view.widget.VoicePlayer;

import org.apache.commons.lang3.StringEscapeUtils;

import lancooControl.Control;
import znbkTopicType.WrapBaseAdapter;
import znbkTestPaperInfor.ZnbkPaperDef;

import static znbkTestPaperCtfd.activity.WrongTopicTutoringPaperActivity.mEntityCtfd;
import static znbkTestPaperNtxl.activity.TrainingDifficultProblemsPaperActivity.mEntityNtxl;
import static znbkTestPaperXtfx.activity.SystemReviewPaperActivity.mEntityXtfx;

/**
 * Created by Administrator on 2018\2\26 0026.
 */

public class ZnbkContentTlxzAdapter extends WrapBaseAdapter{

    private LayoutInflater mInflater;
    //当前试题数量
    private int ItemCount;
    //当前试题ID
    private int PaperID;
    private int ModelFrom;//试卷来源
    private Context mContext;


    private ViewHolder holder = null;
    public final class ViewHolder {
        WebView WvTheme;
        TextView TvListenContent;
        ImageButton BtnPlayPause;
        TextView TvAnswerTime;
        SeekBar SbMusic;
        private ViewHolder(View convertView) {
            WvTheme = (WebView) convertView.findViewById(R.id.wv_theme);
            TvListenContent = (TextView) convertView.findViewById(R.id.tv_listenContent);
            BtnPlayPause = (ImageButton) convertView.findViewById(R.id.btn_start);
            TvAnswerTime = (TextView) convertView.findViewById(R.id.tv_answerTime);
            SbMusic = (SeekBar) convertView.findViewById(R.id.music_progress);
        }
    }

    public ZnbkContentTlxzAdapter(Context context, int itemCount, int itemID, int modelFrom) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.ItemCount = itemCount;
        this.PaperID = itemID;
        this.ModelFrom= modelFrom;
    }

    @Override
    public int getCount() {
        return ItemCount;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.znbk_adapter_content_tlxz_item, null);
            holder = new ViewHolder(convertView);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        /*
      * 获取试题信息
      * */
        getContentInfor();

        /*
        * 获取题目信息
        * */
        showContentInfor();


        return convertView;
    }


    private String quesBody="";//获取大题题干信息
    private String listenContentText="";//听力原文
    private int status=-1;//试题提交状态
    private String quesAudio="";//音频路径
    private String genreName="";//题目类型
    private double score=0;//小题总分
    private double correctRate=0;//准确率

    /*
  * 获取试题信息
  * */
    private void getContentInfor(){

        if(ModelFrom== ZnbkPaperDef.FROM_SYSTEM_REVIEW) {
            status = mEntityXtfx.getModelSinglePaperInfo().getModelPaperInfo().getAnswerState();
            score = mEntityXtfx.getModelSinglePaperInfo().getModelPaperInfo().getDetail().get(PaperID).getScore();
            genreName = mEntityXtfx.getModelSinglePaperInfo().getModelTKQueList().get(PaperID).getGenreName();
            quesBody = mEntityXtfx.getModelSinglePaperInfo().getModelTKQueList().get(PaperID).getQuesBody();
            listenContentText = "<font color='#47D05A'><middle><middle>" + "(听力原文)" + "</middle></middle></font>" +
                    mEntityXtfx.getModelSinglePaperInfo().getModelTKQueList().get(PaperID).getAudioList().get(0).getArticle();
            quesAudio = mEntityXtfx.getModelSinglePaperInfo().getModelTKQueList().get(PaperID).getAudioList().get(0).getAudioPath();
            if(status!=0) {
                correctRate = mEntityXtfx.getModelItemAnswerDetailList().get(PaperID).getCorrectRate();
            }
        }else if(ModelFrom== ZnbkPaperDef.FROM_WEEK_ASCENSION) {

//            status = mJsonRdqh.getModelPaperInfo().getAnswerState();
//            score = mJsonRdqh.getModelPaperInfo().getDetail().get(PaperID).getScore();
//            typeName = mJsonRdqh.getModelTKQueList().get(PaperID).getTypeName();
//            quesBody = mJsonRdqh.getModelTKQueList().get(PaperID).getQuesBody();
//            listenContentText = "<font color='#47D05A'><middle><middle>" + "(听力原文)" + "</middle></middle></font>" +
//                    mJsonRdqh.getModelTKQueList().get(PaperID).getAudioList().get(0).getArticle();
//            quesAudio = mJsonRdqh.getModelTKQueList().get(PaperID).getAudioList().get(0).getAudioPath();
        }else if(ModelFrom== ZnbkPaperDef.FROM_TRAINING_DIFFICULT_PROBLEMS){

            status = mEntityNtxl.getAnswerState();
            genreName = mEntityNtxl.getTKQue().getGenreName();
            quesBody = mEntityNtxl.getTKQue().getQuesBody();
            listenContentText = "<font color='#47D05A'><middle><middle>" + "(听力原文)" + "</middle></middle></font>" +
                    mEntityNtxl.getTKQue().getAudioList().get(0).getArticle();
            quesAudio = mEntityNtxl.getTKQue().getAudioList().get(0).getAudioPath();
            correctRate = mEntityNtxl.getCorrectRate();
        }else if(ModelFrom == ZnbkPaperDef.FROM_WRONG_TOPIC_TUTORING){
            status = mEntityCtfd.getAnswerState();
            int anotherPaperID = -1;
            if(PaperID==0){
                genreName = mEntityCtfd.getPrimitiveQues().getTKQue().getGenreName();
                quesBody = mEntityCtfd.getPrimitiveQues().getTKQue().getQuesBody();
                listenContentText = "<font color='#47D05A'><middle><middle>" + "(听力原文)" + "</middle></middle></font>" +
                        mEntityCtfd.getPrimitiveQues().getTKQue().getAudioList().get(0).getArticle();
                quesAudio = mEntityCtfd.getPrimitiveQues().getTKQue().getAudioList().get(0).getAudioPath();
                correctRate = mEntityCtfd.getPrimitiveQues().getCorrectRate();
            }else if(PaperID==1){
                anotherPaperID = PaperID-1;
                if(mEntityCtfd.getClassicalQues()!=null){
                    genreName = mEntityCtfd.getClassicalQues().getTKQue().getGenreName();
                    quesBody = mEntityCtfd.getClassicalQues().getTKQue().getQuesBody();
                    listenContentText = "<font color='#47D05A'><middle><middle>" + "(听力原文)" + "</middle></middle></font>" +
                            mEntityCtfd.getClassicalQues().getTKQue().getAudioList().get(0).getArticle();
                    quesAudio = mEntityCtfd.getClassicalQues().getTKQue().getAudioList().get(0).getAudioPath();
                    correctRate = mEntityCtfd.getClassicalQues().getCorrectRate();
                }else{
                    genreName = mEntityCtfd.getRecQues().get(anotherPaperID).getTKQue().getGenreName();
                    quesBody = mEntityCtfd.getRecQues().get(anotherPaperID).getTKQue().getQuesBody();
                    listenContentText = "<font color='#47D05A'><middle><middle>" + "(听力原文)" + "</middle></middle></font>" +
                            mEntityCtfd.getRecQues().get(anotherPaperID).getTKQue().getAudioList().get(0).getArticle();
                    quesAudio = mEntityCtfd.getRecQues().get(anotherPaperID).getTKQue().getAudioList().get(0).getAudioPath();
                    correctRate = mEntityCtfd.getRecQues().get(anotherPaperID).getCorrectRate();
                }
            }else{
                if(mEntityCtfd.getClassicalQues()!=null){
                    anotherPaperID = PaperID-2;
                }else{
                    anotherPaperID = PaperID-1;
                }
                genreName = mEntityCtfd.getRecQues().get(anotherPaperID).getTKQue().getGenreName();
                quesBody = mEntityCtfd.getRecQues().get(anotherPaperID).getTKQue().getQuesBody();
                listenContentText = "<font color='#47D05A'><middle><middle>" + "(听力原文)" + "</middle></middle></font>" +
                        mEntityCtfd.getRecQues().get(anotherPaperID).getTKQue().getAudioList().get(0).getArticle();
                quesAudio = mEntityCtfd.getRecQues().get(anotherPaperID).getTKQue().getAudioList().get(0).getAudioPath();
                correctRate = mEntityCtfd.getRecQues().get(anotherPaperID).getCorrectRate();
            }
        }

        mVoicePlayer = new VoicePlayer(holder.SbMusic,quesAudio,holder.TvAnswerTime,holder.BtnPlayPause,true);

    }

    /*
* 获取题目题干信息
* */
    Handler htmlHandlerListenContent= new Handler();
    private void showContentInfor(){

        quesBody = StringEscapeUtils.unescapeXml(quesBody);
        holder.WvTheme.loadDataWithBaseURL("about:blank",quesBody,"text/html","utf-8",null);

//        if(status == 2||status == 3||
//                (ModelFrom==ZnbkPaperDef.FROM_WRONG_TOPIC_TUTORING&PaperID==0)||
//                (ModelFrom==ZnbkPaperDef.FROM_WRONG_TOPIC_TUTORING&PaperID==1&mEntityCtfd.getClassicalQues()!=null)) {
        if(status == 2||status == 3||
                (ModelFrom==ZnbkPaperDef.FROM_WRONG_TOPIC_TUTORING&PaperID==0)) {
            //显示听力原文
            holder.TvListenContent.setVisibility(View.VISIBLE);
            listenContentText = StringEscapeUtils.unescapeXml(listenContentText);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final Spanned tvlistenContentText = Html.fromHtml(listenContentText, Control.imgGetter, null);
                    htmlHandlerListenContent.post(new Runnable() {
                        @Override
                        public void run() {
                            holder.TvListenContent.setText(tvlistenContentText);
                        }
                    });
                }
            }).start();
        }else{
            holder.TvListenContent.setVisibility(View.GONE);
        }

    }

}